﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Test_2D
{
    public partial class AddCom : Form
    {
        public AddCom()
        {
            InitializeComponent();
            height = this.Height;
            width = this.Width;

            
            const string source = @"walk.png";
            this.textBox1.Text = source;
            this.textBox2.Text = "6";
            this.textBox3.Text = "元件";
        }

        int width, height;
        Image img;

        public bool newcom = false;

        Component com;

        int act;

        public Component Component
        {
            get { return com; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        //预览
        private void button3_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                try
                {
                    this.Width = width;
                    this.Height = height;

                    img = Image.FromFile(textBox1.Text);
                    com = new Component(textBox1.Text, act, textBox3.Text.Trim());

                    timer1.Start();

                    MyFunc.drawCom(pictureBox1, com);
                    button2.Enabled = true;

                }
                catch (Exception)
                {
                    MessageBox.Show("出现错误");
                }
            }
        }

        //验证
        private bool validate()
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入元件路径");
                return false;
            }
            if (textBox2.Text.Trim() == "")
            {
                MessageBox.Show("请输入元件的动作数");
                return false;
            }
            if (textBox3.Text.Trim() == "")
            {
                MessageBox.Show("请输入元件名称");
                return false;
            }
            try
            {
                act = Convert.ToInt32(textBox2.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("元件的动作数不正确");
                return false;
            }

            return true;
        }

        //预览滑动效果
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Height - height < img.Height + 5)
            {
                this.Height += 5;
            }
            else
            {
                timer1.Stop();
            }
        }
        //添加按钮
        private void button2_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                newcom = true;
                this.Close();
            }
        }

        private void text_Changed(object sender, EventArgs e)
        {
            this.button2.Enabled = false;
        }
    }
}
